home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_02_06
/
2n06059a
< prev
next >
Wrap
Text File
|
1991-04-30
|
14KB
|
605 lines
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <graph.h>
#include <string.h>
#include <ctype.h>
#include "StdWin.h"
/* malloc statics */
static WORD LocalEntries;
static WORD LocalTotalSize=0;
static WORD GlobalEntries;
static WORD GlobalTotalSize=0;
static VOID **LocalMemPtrs;
static HANDLE *LocalMemHandles;
static HANDLE *GlobalMemHandles;
static VOID huge **GlobalMemPtrs;
BYTE TextBuffer[1024];
#define TABSIZE 4
/* two states used by printf */
static int AnsiTextState=TEXT,NewLineState=0;
/* atexit function pointer */
(_cdecl _FAR_ _LOADDS_ *ExitCallFunc)(void);
int ExitCallInit = 0;
/********************************************************/
/* malloc() & halloc() keep a list of all the allocated
pointers and handles so that they can later be freed */
/********************************************************/
void *malloc(size)
size_t size;
{
static HANDLE MemHandle,MemPtr;
static void *Ptr;
if(!LocalTotalSize) /* not initialized */
{
MemHandle = LocalAlloc(LMEM_MOVEABLE,50);
if(!MemHandle) return(0);
MemPtr = LocalAlloc(LMEM_MOVEABLE,50);
if(!MemPtr) return(0);
LocalTotalSize = 25;
}
if(LocalEntries >= LocalTotalSize && LocalEntries != 0)
{
MemHandle = LocalReAlloc(MemHandle,100,LMEM_MOVEABLE);
if(!MemHandle) return(0);
MemPtr = LocalReAlloc(MemPtr,100,LMEM_MOVEABLE);
if(!MemPtr) return(0);
LocalTotalSize = 50;
}
LocalMemHandles = (HANDLE *)LocalLock(MemHandle);
*LocalMemPtrs = LocalLock(MemPtr);
if(!LocalMemHandles || !*LocalMemPtrs) return(0);
if(!(LocalMemHandles[LocalEntries] =
LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,size)))
{
LocalCompact(0xffff);
if(!(LocalMemHandles[LocalEntries] =
LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,size)))
return(0);
}
if(!(Ptr = LocalLock(LocalMemHandles[LocalEntries])))
return(0);
LocalMemPtrs[LocalEntries] = Ptr;
LocalEntries++;
LocalUnlock(MemHandle);
LocalUnlock(MemPtr);
return(Ptr);
}
/***************************************
function : halloc
***************************************/
void far *halloc(n,size)
long n;
size_t size;
{
static VOID huge *Ptr;
static HANDLE MemHandle,MemPtr;
if(!GlobalTotalSize) /* not initialized */
{
MemHandle = LocalAlloc(LMEM_MOVEABLE,50);
if(!MemHandle) return(0);
MemPtr = LocalAlloc(LMEM_MOVEABLE,50);
if(!MemPtr) return(0);
GlobalTotalSize = 25;
}
if(GlobalEntries >= GlobalTotalSize && GlobalEntries != 0)
{
MemHandle = LocalReAlloc(MemHandle,GlobalTotalSize+50,
LMEM_MOVEABLE);
if(!MemHandle) return(0);
MemPtr = LocalReAlloc(MemPtr,GlobalTotalSize + 50,
LMEM_MOVEABLE);
if(!MemPtr) return(0);
GlobalTotalSize += 25;
}
GlobalMemHandles = (HANDLE *)LocalLock(MemHandle);
*GlobalMemPtrs = LocalLock(MemPtr);
if(!GlobalMemHandles || !*GlobalMemPtrs) return(0);
if(!(GlobalMemHandles[GlobalEntries] =
GlobalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,
(long)size*n)))
{
GlobalCompact(-1L);
if(!(GlobalMemHandles[GlobalEntries] =
GlobalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,
(long)size*n)))
return(0);
}
if(!(Ptr =
(VOID FAR *)GlobalLock(GlobalMemHandles[GlobalEntries])))
return(0);
GlobalMemPtrs[GlobalEntries] = Ptr;
GlobalEntries++;
LocalUnlock(MemHandle);
LocalUnlock(MemPtr);
return(Ptr);
}
/***************************************
function : free
***************************************/
void free(ptr)
void *ptr;
{
int i = 0;
while(i < LocalEntries)
{
if(LocalMemPtrs[i] == ptr)
{
LocalUnlock(LocalMemHandles[i]);
LocalFree(LocalMemHandles[i]);
return;
}
i++;
}
}
/***************************************
function : hfree
***************************************/
void hfree(hugeptr)
void huge *hugeptr;
{
int i = 0;
while(i < GlobalEntries)
{
if(GlobalMemPtrs[i] == hugeptr)
{
GlobalUnlock(GlobalMemHandles[i]);
GlobalFree(GlobalMemHandles[i]);
return;
}
i++;
}
}
/***************************************
function : printf
***************************************/
int printf(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,
arg9,arg10,arg11,arg12,arg13,arg14,arg15,arg16,arg17,
arg18,arg19,arg20,arg21,arg22)
const char _FAR_ *str;
WORD arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,
arg11,arg12,arg13,arg14,arg15,arg16,arg17,arg18,arg19,
arg20,arg21,arg22;
{
HDC hDC;
HANDLE hOldFont,hFont;
DWORD dwExtent;
int n,i,CharCount;
static BYTE number[8];
MSG msg;
/* 1 of 2 external message loops not found in
WinMain(), another in getch*/
Loop:
/* clear the message queue */
while(PeekMessage(&msg,NULL,0,0xFFFF,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);}
/* if the break key is pressed, hold for another key */
if(BreakSet) goto Loop;
CharCount = sprintf(TextBuffer,str,arg1,arg2,arg3,arg4,
arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12,arg13,arg14,
arg15,arg16,arg17,arg18,arg19,arg20,arg21,arg22);
/* see if output is redirected or piped */
if(hOutFile)
write(hOutFile,TextBuffer,CharCount);
if(Redirected) return(CharCount);
hDC=GetDC(hStdWinWnd);
SetMyDC(hDC);
ptr = TextBuffer;
while(*ptr)
{
/* set up the AnsiTextState to start with */
if((*ptr == 0x1b) && (*(ptr + 1) == '['))
AnsiTextState = ANSI;
else AnsiTextState = TEXT;
switch(AnsiTextState){
case TEXT:
hFont = hFont1; /* default font */
if(*ptr)
{ int i;
BYTE buffer[120];
hOldFont = SelectObject(hDC, hFont);
CharCount = 0;
ptr2 = ptr;
while(*ptr2 == '\b') /* process a backspace */
{memmove(ptr2,ptr2+1,strlen(ptr2));
if(DisplayCol){
xCurrentPos -= ScreenBuffer[DisplayLine]
[DisplayCol].xExtent;
DisplayCol--;
HideCaret(hStdWinWnd);
TextOut(hDC,xCurrentPos,yCurrentPos," ",1);
ShowCaret(hStdWinWnd);
}
}
/* advance until a control character */
while(*ptr2)
{
if((*ptr2 == 0x1b) && (*(ptr2 + 1) == '['))
break;
if((*ptr2 == 0x0d) || (*ptr2 == 0x0a))
break;
else NewLineState = 0;
if(*ptr2 == '\b')
break;
if((DisplayCol + CharCount) >= MAXCOLUMNS)
break;
CharCount++;
if(*ptr == '\t') /* expand tab to spaces */
{
i = strlen(TextBuffer);
memmove(ptr2 + TABSIZE-1,ptr2,
i - (ptr2-TextBuffer)+1);
i = TABSIZE;
while(i--) *ptr2++ = ' ';
CharCount += TABSIZE-1;
}
ptr2++;
}
if(DisplayLine >= MAXROWS)
yCurrentPos = Scrollup(hStdWinWnd,yCurrentPos);
HideCaret(hStdWinWnd);
TextOut(hDC,xCurrentPos,yCurrentPos,ptr,CharCount);
xCurrentPos +=
LOWORD(GetTextExtent(hDC,ptr,CharCount));
SetCaretPos(xCurrentPos,yCurrentPos);
ShowCaret(hStdWinWnd);
/* copy the new characters to the screen buffer */
for(i = DisplayCol;i < DisplayCol + CharCount;i++)
{ ScreenBuffer[DisplayLine][i].Char = *ptr;
ScreenBuffer[DisplayLine][i].xExtent =
LOWORD(GetTextExtent(hDC,ptr++,1));
ScreenBuffer[DisplayLine][i].Color =
rgbTextColor;
ScreenBuffer[DisplayLine][i].hFont = hFont;
if(i < MAXCOLUMNS){
ScreenBuffer[DisplayLine][i+1].Char = 0;
ScreenBuffer[DisplayLine][i+1].Color = 0L;
ScreenBuffer[DisplayLine][i+1].hFont = 0;
}
}
DisplayCol += CharCount;
if((DisplayCol >= MAXCOLUMNS) && !iscntrl(*ptr2)){
/* force a newline for a wrap */
NewLineState = 1;
DisplayCol = 0;
if(hOutFile) write(hOutFile,"\r",1);
xCurrentPos = 0;
}
if(*ptr2 == 0x0d){
DisplayCol = 0;
if(hOutFile) write(hOutFile,"\r",1);
xCurrentPos = 0;
if(!NewLineState) ptr2++;
NewLineState = 1;}
if((*ptr2 == 0x0a) || NewLineState)
{
DisplayLine++;
if(!NewLineState)
{
DisplayCol = 0;
if(hOutFile) write(hOutFile,"\r",1);
xCurrentPos = 0;
}
if(*ptr2 == 0x0a) ptr2++;
NewLineState = 0;
dwExtent = GetTextExtent(hDC,"A",1);
yCurrentPos += LineExtents[DisplayLine] =
HIWORD(dwExtent);
HideCaret(hStdWinWnd);
SetCaretPos(xCurrentPos,yCurrentPos);
ShowCaret